package com.bjsxt.shop.common.util;

import java.util.Date;

import javax.annotation.Resource;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author WYN Redis的工具类
 */
@Component("redisUtil")
public class RedisUtil {
	@Resource
	private RedisTemplate redisTemplate;
	//给redis设置值
	@SuppressWarnings("unchecked")
	public boolean set(final String key,final String value){
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					return true ; 
				} catch (Exception e)
				{
				}
				return false;
			}
		});
	}
	
	/**
	 * 给redis设置值，设置键值对，并且设定过期时间
	 * 指定的时间单位是秒
	 */
	@SuppressWarnings("unchecked")
	public boolean set(String key,String value, int timeout){
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					/* 指定一下过期时间 */
					return connection.expire(keyBytes, timeout);
				} catch (Exception e)
				{
				}
				return false;
			}
		});
	}
	/**
	 * 设置键值对，指定那个时间点过期
	 */
	@SuppressWarnings("unchecked")
	public boolean set(final String key,final String value, final Date timeout)
	{
		return (boolean) this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueBytes = redisTemplate.getStringSerializer().serialize(value);
					connection.set(keyBytes, valueBytes);
					/* 指定一下过期时间 */
					return connection.expireAt(keyBytes, timeout.getTime());
				} catch (Exception e)
				{
				}
				return false;
			}
		});
	}
	
	/**
	 * 通过key取值
	 */
	@SuppressWarnings("unchecked")
	public String get(final String key){
		return ""+this.redisTemplate.execute(new RedisCallback() {

			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException {
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					byte[] valueByte = connection.get(keyBytes);
					/* 将字符数组转换成字符串 */
					String value = redisTemplate.getStringSerializer().deserialize(valueByte) + "";
					return value ; 
				} catch (Exception e)
				{
					
					return "";
				}
			}
		});
	}
	/**
	 * 通过键删除值
	 */
	@SuppressWarnings("unchecked")
	public String delete(final String key)
	{
		return "" + this.redisTemplate.execute(new RedisCallback()
		{
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException
			{
				try
				{
					/*connection.set(key.getBytes("UTF-8"), value.getBytes("UTF-8"));*/
					/* 使用Redis提供的工具类,将字符串转换成字节数组 */
					byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key);
					return connection.del(keyBytes) ; 
				} catch (Exception e)
				{
				}
				return "";
			}
		});
	}
}
